const { User } = require('../../model/user');
const bcrypt = require('bcryptjs');
module.exports = async(req, res, next) => {
    const user = await User.findOne({ _id: req.query.id });
    if (user) {
        const { password, username, email, state, role } = req.body;
        let isEquals = await bcrypt.compare(password, user.password);
        console.log(isEquals);
        if (isEquals) {
            await User.updateOne({ _id: req.query.id }, { username, email, state, role });
            res.redirect('/admin/user');
        } else {
            next(JSON.stringify({ path: '/admin/user-edit', id: req.query.id, message: '密码不匹配,不能进行编辑' }));
        }
    } else {
        next('用户不存在,不能进行编辑');
    }
}